package com.pachong.android.frameworkbase.customviews;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ClickableSpan;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import com.pachong.android.frameworkbase.R;
import com.pachong.android.frameworkbase.utils.BitmapUtil;

/**
 * 一个可以显示image，点击image响应的EditText
 * 
 * 添加功能：添加录音图标  
 * 
 * @author Veivei Peng
 * 
 */
public class ImageEditText extends EditText {

	private static final String TAG = ImageEditText.class.getSimpleName();
	private int mReqImgWidth = 400;
	private int mReqImgHeight = 400;


	public ImageEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		if (attrs != null) {
			TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ImageEditText);
			mReqImgWidth = (int)a.getDimension(R.styleable.ImageEditText_request_width, 400);
			mReqImgHeight = (int)a.getDimension(R.styleable.ImageEditText_request_height, 400);
			a.recycle();
		}
	}


	/**
	 * 追加图片， imgPath == /sdcard/dddd.png
	 * 
	 * @param imgPath
	 */
	private void appendImage(String imgPath) {
		if(TextUtils.isEmpty(imgPath)) {
			return;
		}
		
		// TODO: 更多的异常判断
		this.append(getDrawableStr(imgPath));
		this.append("  "); // 防止触摸太敏感
		Log.e("fycus", "no no no back back");
		
	}  

	private CharSequence getDrawableStr(String picPath) {
		String str = IMG_START + picPath + IMG_END;
		final SpannableString ss = new SpannableString(str);
		// 定义插入图片
		Bitmap bm = BitmapUtil.decodeBitmapFromFilePath(picPath, mReqImgWidth, mReqImgHeight);
		Drawable drawable = new BitmapDrawable(bm);
		drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
		ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);
		ss.setSpan(span, 0, ss.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
		
		if (mImageListener != null) {
			ss.setSpan(new MyClickableSpan(picPath), 0, ss.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
		}
		return ss;

	}

	public class MyClickableSpan extends ClickableSpan {

		String imgSrc = null;

		MyClickableSpan(String imgSrc) {
			this.imgSrc = imgSrc;
		}

		@Override
		public void onClick(View widget) {
			if (mImageListener != null && !TextUtils.isEmpty(imgSrc)) {
				mImageListener.onImageClicked(imgSrc);
			}
		}
	}

	public static final String IMG_START = "<<<";
	public static final String IMG_END = ">>>";

	/**
	 * 从content解析出图片标签<img>，并设置到EditText中。
	 * 算法：假设 aaaaa, <img src="/mnt/sdcard/xxx.png"/> bbbbbbbb————<img src="/mnt/sdcard/yyyy.png/>dddddddd
	 * 改方法将“aaa”到第一个“/>”为止作为一个处理单元，然后再处理“bbbb”，到第二个“/>”，这两个步骤以及接下去的的处理方式其实是一样的，所以我用了递归。
	 *  
	 * @param content
	 */
	public void setContent(String content) {
		// 递归出口
		if (TextUtils.isEmpty(content)) {
			return;
		}
//		Log.e(TAG, "content == " + content);

		int startIndex = 0, endIndex = 0;
		int imgStartIndex = content.indexOf(IMG_START);
		if (imgStartIndex < 0) {// 没有<img>标签，说明没有图片了
			endIndex = content.length();
		} else {
			endIndex = imgStartIndex;
		}

		String str = content.substring(startIndex, endIndex);
		Log.e(TAG, "String1 == " + str);

		this.append(str);
		content = content.substring(endIndex, content.length());// 将变量str表示的字符串删除
		Log.e(TAG, "content == " + content);

		// 设置 img
		if (TextUtils.isEmpty(content)) {
			return;
		}

		int imgEndIndex = content.indexOf(IMG_END);
		str = content.substring(IMG_START.length(), imgEndIndex);
		Log.e(TAG, "String2 == " + str);
		appendImage(str);

		content = content.substring(imgEndIndex + IMG_END.length(), content.length());// 将变量str表示的字符串删除
		Log.e(TAG, "content == " + content);
		setContent(content);

	}

	private OnImageClickedListener mImageListener = null;

	public void setOnImageClickedListener(OnImageClickedListener listener) {
		if (listener != null) {
			mImageListener = listener;
		}
	}

	public static class OnImageClickedListener {

		public void onImageClicked(String imgSrc) {
			if (TextUtils.isEmpty(imgSrc)) {
				Log.e(TAG, "Image src is null");
				return;
			}
		}
	}
	

}
